// pages/travel/index.js
import {
    platform
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selAct: 0,
        value: '',
        weatherInfo: '',
        InSeasonList: [],
        districtList: [],
        district_id: '',
        active: 0,
        loadMoreText: '加载中...', //底部文字
        list: [],
        distanceMy: '',
        distance: '',
        selAct2: '',
        orderway: '',
        orderby: '',
        value1: 0,
        value2: 'a',
        option1: [{
                text: '默认排序',
                value: 0
            },
            {
                text: '距离排序',
                value: 1
            },
            {
                text: '评分排序',
                value: 2
            },
        ],
        option2: [{
                text: '排序方式',
                value: 'a'
            },
            {
                text: '从低到高',
                value: 'b'
            },
            {
                text: '从高到低',
                value: 'c'
            },
        ],
        latitude: '',
        longitude: '',
        categoryList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getCategory()
    },
    getCategory() {
        platform.scenic.category.lists({}, res => {
            if (res.code == 1) {
                this.setData({
                    categoryList: res.data,
                    category_id:res.data[0].id
                })
                this.getList(1)
            }
        })
    },
    change1() {
        if (this.data.value1 == 1) {
            this.setData({
                orderby: 'distance'
            })
        }
        if (this.data.value1 == 2) {
            this.setData({
                orderby: 'score'
            })
        }
        if (this.data.value1 == 0) {
            this.setData({
                orderby: ''
            })
        }
        this.onPullDownRefresh()
    },
    change2() {
        if (this.data.value2 == 'a') {
            this.setData({
                orderway: ''
            })
        }
        if (this.data.value2 == 'b') {
            this.setData({
                orderway: 'asc'
            })
        }
        if (this.data.value2 == 'c') {
            this.setData({
                orderway: 'desc'
            })
        }
        this.onPullDownRefresh()
    },
    onChange(e) {
        this.setData({
            active: e.detail.name,
            category_id:e.detail.name
        })
        this.onPullDownRefresh()
    },
    // 搜索
    onClick() {
        console.log(this.data.value)
        this.onPullDownRefresh()
    },
    toCall(e){
        let phone = e.currentTarget.dataset.phone
        wx.makePhoneCall({
          phoneNumber: phone,
        })
    },
    toAct(e) {
        let act = e.currentTarget.dataset.act
        if (this.data.selAct == act) {
            this.setData({
                selAct: 0
            })
        } else {
            this.setData({
                selAct: act
            })
        }
        this.onPullDownRefresh()
    },
    toAct2(e) {
        let act = e.currentTarget.dataset.act
        if (this.data.selAct2 == act) {
            this.setData({
                selAct2: 0
            })
        } else {
            this.setData({
                selAct2: act
            })
        }

    },
    toAct3(e) {
        let type = e.currentTarget.dataset.type
        this.setData({
            selAct2: 0,
            orderway: type
        })
        this.onPullDownRefresh()
    },
    toOrder(e){
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
          url: '/pages/travel/guide?id='+id,
        })
    },
    // 获取列表
    getList(page) {
        let that = this
        if(that.data.category_id==3){
            platform.guide.lists({
                page: page,
                size: 10,
                name: this.data.value
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    wx.getLocation({
                        type: 'wgs84',
                        success(re) {
                            const latitude = re.latitude
                            const longitude = re.longitude
                            res.data.list.forEach(item => {
                                item.distance_new = that.distance(latitude, longitude, item.latitude, item.longitude)
                            })
                            that.setData({
                                latitude,
                                longitude,
                                list: that.data.list.concat(res.data.list) //合并数据
                            })
                            if (11 > res.data.list.length > 0) {
                                that.setData({
                                    loadMoreText: '' //没有更多了
                                })
                            }
                        },
                        fail(re) {
                            that.setData({
                                list: that.data.list.concat(res.data.list) //合并数据
                            })
                            if (11 > res.data.list.length > 0) {
                                that.setData({
                                    loadMoreText: '' //没有更多了
                                })
                            }
                            // wx.showToast({
                            //     title: '定位失败',
                            //     icon: 'error'
                            // })
                        }
                    })
    
    
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }else{
            platform.scenic.lists({
                page: page,
                size: 10,
                name: this.data.value,
                orderway: this.data.orderway,
                orderby: this.data.orderby,
                latitude: this.data.latitude,
                longitude: this.data.longitude,
                category_id:this.data.category_id
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    wx.getLocation({
                        type: 'wgs84',
                        success(re) {
                            const latitude = re.latitude
                            const longitude = re.longitude
                            res.data.list.forEach(item => {
                                item.distance_new = that.distance(latitude, longitude, item.latitude, item.longitude)
                            })
                            that.setData({
                                latitude,
                                longitude,
                                list: that.data.list.concat(res.data.list) //合并数据
                            })
                            if (11 > res.data.list.length > 0) {
                                that.setData({
                                    loadMoreText: '' //没有更多了
                                })
                            }
                        },
                        fail(re) {
                            that.setData({
                                list: that.data.list.concat(res.data.list) //合并数据
                            })
                            if (11 > res.data.list.length > 0) {
                                that.setData({
                                    loadMoreText: '' //没有更多了
                                })
                            }
                            // wx.showToast({
                            //     title: '定位失败',
                            //     icon: 'error'
                            // })
                        }
                    })
    
    
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }
       
    },
    // 计算距离
    // la1用户的纬度
    // lo1用户的经度
    // la2商家的纬度
    // lo2商家的经度
    // 计算距离函数
    distance(la1, lo1, la2, lo2) {
        var La1 = (la1 * Math.PI) / 180.0
        var La2 = (la2 * Math.PI) / 180.0
        var La3 = La1 - La2
        var Lb3 = (lo1 * Math.PI) / 180.0 - (lo2 * Math.PI) / 180.0
        var s =
            2 *
            Math.asin(
                Math.sqrt(
                    Math.pow(Math.sin(La3 / 2), 2) +
                    Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)
                )
            )
        s = s * 6378.137
        s = Math.round(s * 10000) / 10000
        s = s.toFixed(2)
        return s
    },
    toDis(e) {
        console.log(e)
        this.setData({
            distance: e.currentTarget.dataset.num,
            selAct: 0
        })
        this.onPullDownRefresh()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})